﻿using Blog.Core.IServices;
using Blog.Core.Model;
using Blog.Core.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Blog.Core.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class chapterController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IchapterServices _chapterServices;

        public chapterController(IchapterServices chapterServices)
        {
            _chapterServices = chapterServices;
        }

        [HttpGet]
        public async Task<MessageModel<PageModel<chapter>>> Get(int page = 1, int bId = 0,string cName="", int intPageSize = 10000)
        {
            Expression<Func<chapter, bool>> whereExpression = a => true;
            if (string.IsNullOrEmpty(cName))
            {
                whereExpression = a => a.bId == bId;
            }
            else
            {
                whereExpression = a => a.bId == bId && a.cName.Contains(cName);
            }
            PageModel<chapter> chapters = await _chapterServices.QueryPage(whereExpression, page, intPageSize);
            for (int i = 0; i < chapters.data.Count; i++)
            {
                chapters.data[i].cContent = "";
            }
            return new MessageModel<PageModel<chapter>>()
            {
                msg = "获取成功",
                success = true,
                response = chapters
            };

        }

        [HttpGet]
        public async Task<MessageModel<chapter>> GetModel(int id)
        {
            chapter chapter = await _chapterServices.QueryById(id);
            int begin = 0;
            int end = -1;
            List<chapter> chapters = await _chapterServices.Query(c => c.bId == chapter.bId, " bId asc ");
            for (int i = 0; i < chapters.Count; i++)
            {
                if (chapters[i].cId == id)
                {
                    if (i > 0)
                    {
                        begin = chapters[i - 1].cId;
                    }
                    if (chapters.Count - 1 - i > 0)
                    {
                        end = chapters[i + 1].cId;
                    }
                    break;
                }
            }

            return new MessageModel<chapter>()
            {
                msg = "获取成功",
                success = true,
                response = chapter,
                msgDev = begin + "|" + end
            };
        }
        [HttpGet]
        public async Task<MessageModel<chapter>> GetFirst(int bId)
        {
            List<chapter> chapters = await _chapterServices.Query(c => c.bId == bId, " bId asc ");
            chapter chapter = new chapter();
            int end = -1;
            if (chapters.Count > 0)
            {
                chapter = chapters[0];
                if (chapters.Count > 1)
                {
                    end = chapters[1].cId;
                }
            }
            return new MessageModel<chapter>()
            {
                msg = "获取成功",
                success = true,
                response = chapter,
                msgDev = "0|" + end.ToString()
            };
        }
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] chapter request)
        {
            var data = new MessageModel<string>();

            var id = await _chapterServices.Add(request);
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }

        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] chapter request)
        {
            var data = new MessageModel<string>();
            data.success = await _chapterServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.bId.ObjToString();
            }

            return data;
        }

        [HttpDelete("{id}")]
        public async Task<MessageModel<string>> Delete(string id)
        {
            var data = new MessageModel<string>();
            data.success = await _chapterServices.DeleteById(id);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = id;
            }

            return data;
        }
    }
}